package servlets;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import webservices.helper.Helper;
import control.ControlService;

/**
 * Servlet implementation class CourseManageServlet
 */
public class SlideQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SlideQueryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}
	
	private void processRequest(HttpServletRequest request, HttpServletResponse response){
		try{
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e){}
		
		String requestXML = Helper.paramStr("courseID", Helper.getParam(request, "courseID"));
		
		String responseXML = new ControlService().Call("SlideQuery", requestXML);
		
		try {
			NodeList responseList = Helper.getResponse(responseXML);
			
			String json = "[";
			
			for (int i = 0; i < responseList.getLength(); i++) {
				Element r = (Element)responseList.item(i);
				String name = r.getAttribute("name");
				if ("slide".equals(name)){
					if ( i > 1 ){
						json += ",";
					}
					json += "{slideID: '" + r.getElementsByTagName("slideID").item(0).getTextContent() + "',";
					json += "name: '" + r.getElementsByTagName("name").item(0).getTextContent() + "',";
					json += "description: '" + r.getElementsByTagName("description").item(0).getTextContent() + "',";
					json += "link: '" + r.getElementsByTagName("link").item(0).getTextContent() + "'}";
				}			
			}
			json += "]";
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(json);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
